#include <iostream>
#include <string>
#include "common.h"
#include <stdint.h>

using namespace std;

class A
{
    private:
        int msg_;
    public:
        virtual void print()
        {
            cout << " 这是一个父类 " << endl;
        }
};

class B : public A
{
    public:
        virtual void print()
        {
            cout << " 这是一个子类 " << endl;
        }
};

int main(int argc, char *argv[])
{

    { // static_cast
        __LOG__("static_cast");
        double a1 = 34.8;

        int a2 = 10;

        char a3[] = "Hubery";

        char *a4 = "Tom";

        cout << "a2 转换前: " << a2 << endl;
        cout << "a4 转换前: " << a4 << endl;

        a2 = static_cast<int>(a1);
        // a2 = static_cast<int>(a3); 错误
        a4 = static_cast<char *>(a3);

        cout << "a2 转换后: " << a2 << endl;
        cout << "a4 转换后: " << a4 << endl;

    }



    { // reinterpret_cast
        __LOG__("reinterpret_cast");

        int num = 0x00636261;//用16进制表示32位int，0x61是字符'a'的ASCII码
        int * pnum = &num;
        char * pstr = reinterpret_cast<char *>(pnum);
        cout<<"pnum指针的值: "<<pnum<<endl;
        cout<<"pstr指针的值: "<<static_cast<void *>(pstr)<<endl;//直接输出pstr会输出其指向的字符串，这里的类型转换是为了保证输出pstr的值
        cout<<"pnum指向的内容: "<<hex<<*pnum<<endl;
        cout<<"pstr指向的内容: " << pstr<<endl;

        char c = 'a';
        
        int c1 = static_cast<int>(c);
        
        cout << hex << c1 << endl;

        uint8_t d = 97;

        char c2 = static_cast<char>(d);
        cout << c2 << endl;

    }

    {
        // static_cast
        __LOG__("static_cast");
        volatile const int a = 3;
        int* pa = const_cast<int*>(&a);
        *pa = *pa+1;
        cout << a << endl;   //3
        cout << (*pa) << endl;  //4


    }

    {
        __LOG__("dynamic_cast");
        A a;
        B b;
        A* pa = &a;    //父类指针指向父类对象
        A* pb = &b;    //父类指针指向子类对象
        B* pA = dynamic_cast<B*>(pa);   //转换失败
        B* pB = dynamic_cast<B*>(pb);   //转换成功
        cout << "a: " << &a << endl;
        cout << "b: " << &b << endl;
        cout << "pA: "<<pA << endl;
        cout << "pB: " << pB << endl;

    }

    return 0;
}

